/**
 * 实现一个十进制数字的二进制输出
 * 利用栈结构
 */

function toBin(figure: number): string {
  if (figure <= 0) {
    throw new Error('must be a positive integer')
  }
  let arr: number[] = []
  while (figure) {
    figure % 2 ? arr.push(1) : arr.push(0)
    figure = Math.floor(figure / 2)
  }
  let res = ''
  while (arr.length) {
    res += arr.pop()
  }
  return res
}

const res = toBin(127)
console.log(res) //1111111
